package com.hit.server.biz.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hit.server.api.entity.CheckStandardEntity;
import com.hit.server.api.dto.CheckStandardQueryDTO;
import com.hit.server.api.dto.CheckStandardSaveDTO;
import com.hit.server.api.dto.CheckStandardUpdateDTO;
import com.hit.server.api.entity.CheckTypeEntity;
import com.hit.server.api.vo.CheckStandardVO;
import com.hit.server.biz.mapper.CheckStandardMapper;
import com.hit.server.api.service.CheckStandardService;
import com.hit.server.biz.mapper.CheckTypeMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
import lombok.RequiredArgsConstructor;

import java.util.List;
import java.util.Optional;

/**
 * 检测标准
 *
 * @author kehanjiang
 * @date 2025-04-15 14:00:28
 */
@Service
@RequiredArgsConstructor
public class CheckStandardServiceImpl extends ServiceImpl<CheckStandardMapper, CheckStandardEntity> implements CheckStandardService {
    private final CheckTypeMapper checkTypeMapper;
	private CheckStandardVO convertToVO(Object o) {
        if (o instanceof CheckStandardEntity) {
            CheckStandardVO vo = new CheckStandardVO();
            BeanUtil.copyProperties(o, vo);
			//设置其他参数
            Optional.ofNullable(vo.getTypeId()).ifPresent(typeId -> {
                CheckTypeEntity checkType = checkTypeMapper.selectById(typeId);
                if (checkType != null) {
                    vo.setTypeName(checkType.getName());
                    vo.setDiscernType(checkType.getDiscernType());
                }
            });
            return vo;
        }
        return null;
    }
	
	private void preCheck(CheckStandardEntity entity, Boolean update) {
        Long id = entity.getId();
        String name = entity.getName();
        if (update) {//更新操作
           Assert.notNull(id, "id不可为空！");
        } else {
           Assert.hasText(name, "name不可为空！");
        }
    }

    @Override
    public Page<CheckStandardVO> getCheckStandardPage(Page page, CheckStandardQueryDTO dto){
        CheckStandardEntity entity = new CheckStandardEntity();
        BeanUtils.copyProperties(dto, entity);
        entity.setName(null);
        final String name = dto.getName();
        LambdaQueryWrapper<CheckStandardEntity> wrapper = new LambdaQueryWrapper<>(entity);
        wrapper.like(StringUtils.hasText(name), CheckStandardEntity::getName, name);
        wrapper.orderByAsc(CheckStandardEntity::getId);
        return (Page<CheckStandardVO>) this.page(page, wrapper)
                .convert(this::convertToVO);
    }
	
    @Override
    public CheckStandardVO getCheckStandardById(Long id){
        Assert.notNull(id, "id不可为空！");
        return convertToVO(this.getById(id));
    }
	
	
    @Override
    public Boolean saveCheckStandard(CheckStandardSaveDTO dto){
        CheckStandardEntity entity = new CheckStandardEntity();
        BeanUtils.copyProperties(dto, entity);
        this.preCheck(entity, false);
        return this.save(entity);
    }
	
    @Override
    public Boolean updateCheckStandardById(CheckStandardUpdateDTO dto){
        CheckStandardEntity entity = new CheckStandardEntity();
        BeanUtils.copyProperties(dto, entity);
        this.preCheck(entity, true);
        return this.updateById(entity);
    }
	
    @Override
    public Boolean removeCheckStandardBatchByIds(List<Long> ids){
        Assert.notEmpty(ids, "ID不可为空！");
        return this.removeBatchByIds(ids);
    }
}